﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileRenamer.GUI
{
    // TODO this should be converted over to being a user control rather than a form. This will constrain the item to the form bounds
    public partial class OutputText : Form
    {
        private string m_rtfText;
        private Timer m_displayTimer = new Timer();
        private Timer m_hoverTimer = new Timer();

        private int m_textMargin = 4;
        public int TextMargin
        {
            get { return m_textMargin; }
            set { m_textMargin = value; }
        }

        public int m_hoverTime = 100;
        public int HoverTime
        {
            get
            {
                return m_hoverTime;
            }
            set
            {
                m_hoverTime = value;
            }
        }

        public string RtfText
        {
            get 
            { 
                return m_rtfText; 
            }
            set 
            {
                richTextBox1.Rtf = m_rtfText = value;
                this.Size = new Size(richTextBox1.PreferredSize.Width + (m_textMargin * 2), richTextBox1.PreferredSize.Height + (m_textMargin * 2));
                richTextBox1.Location = new Point(m_textMargin, m_textMargin);
                richTextBox1.Size = richTextBox1.PreferredSize;
            }
        }

        private bool m_view;
        public bool View
        {
            get
            {
                return m_view;
            }
            set
            {
                m_view = value;

                if (m_view == false)
                {
                    Visible = false;
                    m_hoverTimer.Stop();
                }
            }
        }

        private Point m_lastPosition;
        private Point m_position;

        public Point Position
        {
            get
            {
                return m_position;
            }
            set
            {
                // m_lastPosition = (m_position == null) ? value : m_position;
                m_position = value;
            }
        }

        public OutputText()
        {
            InitializeComponent();

            m_displayTimer.Interval = 100;
            m_displayTimer.Tick += new EventHandler(m_displayTimer_Tick);
         //   m_displayTimer.Start();

            m_hoverTimer.Interval = 500;
           // m_hoverTimer.Tick += new EventHandler(m_hoverTimer_Tick);
        }

        void m_hoverTimer_Tick(object sender, EventArgs e)
        {
            Location = new Point(m_position.X + 20, m_position.Y + 20);

            if (!string.IsNullOrWhiteSpace(richTextBox1.Text))
            {
                Visible = true;
            }

            m_hoverTimer.Stop();
        }

        void m_displayTimer_Tick(object sender, EventArgs e)
        {
            if (m_view == true)
            {
                if (m_lastPosition == m_position)
                {
                    m_hoverTimer.Start();
                }
                else
                {
                    m_lastPosition = Position;
                    m_hoverTimer.Stop();
                    Visible = false;
                }
            }
            else
            {
                Visible = false;
            }
        }
    }
}
